import 'dart:async';

import 'package:demo/widget/my_button.dart';
import 'package:flukit/flukit.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';

import '../../routers/navigators.dart';
import '../../utils/utils.dart';
import '../../res/resources.dart';
import '../login/login_router.dart';

class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SplashPageState();
  }
}

class _SplashPageState extends State<SplashPage> {
  int _state = 0;
  List<Widget> _guideWidgetList = new List();
  StreamSubscription _subscription;

  @override
  void initState() {
    super.initState();
    _initSplash();
  }

  @override
  void dispose() {
    _subscription?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Stack(
        children: <Widget>[
          Offstage(
              //启动页布局
              offstage: !(_state == 0), //false 显示  true 隐藏
              child: Container(
                color: Colorful.color_state,
                child: Center(
                  child: Image.asset(
                    Utils.getImgPath("demo/launch_logo"), //启动图
                    height: 70,
                    width: 270,
                    fit: BoxFit.fill, //展示方式
                  ),
                ),
              )),
          Offstage(
            //引导页布局
            offstage: !(_state == 1),
            child: _guideWidgetList.isEmpty
                ? Container()
                : Swiper(
                    autoStart: false,
                    circular: false,
                    indicator: null,
                    children: _guideWidgetList,
                  ),
          ),
        ],
      ),
    );
  }

  ///启动页
  void _initSplash() {
    _subscription =
        Observable.just(1).delay(Duration(milliseconds: 2500)).listen((_) {
      _toggle();
    });
  }

  ///切换页面
  void _toggle() async {
    await SpUtil.getInstance();
    if (!SpUtil.getBool("hasGuide", defValue: false)) {
      //是否展示过引导页
      SpUtil.putBool("hasGuide", true);
      _showGuide(); //展示引导页
    } else {
      _toLogin(); //展示登陆页
    }
  }

  void _showGuide() {
    _initGuideData();
    setState(() {
      _state = 1;
    });
  }

  ///跳转登陆页
  void _toLogin() {
    Navigators.push(context, LoginRouter.loginPage, replace: true);
  }

  ///初始化引导页图片
  void _initGuideData() {
    List<String> _guideList = [
      Utils.getImgPath("demo/bg_class1"),
      Utils.getImgPath("demo/bg_class2"),
      Utils.getImgPath("demo/bg_class3"),
      Utils.getImgPath("demo/bg_class4"),
      Utils.getImgPath("demo/bg_class5"),
      Utils.getImgPath("demo/bg_class6"),
    ];

    for (int i = 0; i < _guideList.length; i++) {
      if (i == _guideList.length - 1) {
        //最后一页添加点击事件
        _guideWidgetList.add(InkWell(
          onTap: () {
            _toLogin();
          },
          child: Stack(
            children: <Widget>[
              Image.asset(
                _guideList[i],
                fit: BoxFit.fill,
                width: double.infinity,
                height: double.infinity,
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(left: 30, right: 30, bottom: 100),
                    child: MyButton(
                      text: "开启应用",
                      onPressed: _toLogin,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ));
      } else {
        _guideWidgetList.add(Image.asset(
          _guideList[i],
          fit: BoxFit.fill,
          width: double.infinity,
          height: double.infinity,
        ));
      }
    }
  }
}
